﻿Public Class frmContatos
#Region "Declaração de variáveis"
    Private prvIDContato As Int32
    Private prvIDCliente As Int32
    Private prvIdDepto As Int32
    Private prvDepto As String
    Private prvNome As String
    Private prvTelefone As String
    Private prvCelular As String
    Private prvFax As String
    Private prvEmail As String
    Private prvPrincipal As Boolean
    Private drContato As DataRow
    Private sNomeCliente As String
    Private sMensagemValidacao As String = ""

    Private objContato As New Controle.ctrContato
    Private objDepartamento As New Controle.ctrDepartamento
#End Region

#Region "Propriedades"
    Public Property IDContato()
        Get
            IDContato = prvIDContato
        End Get
        Set(ByVal value)
            prvIDContato = value
        End Set
    End Property
    Public Property Contatos()
        Get
            Return drContato
        End Get
        Set(ByVal value)
            drContato = value
        End Set
    End Property
    Public Property IDCliente()
        Get
            IDCliente = prvIDCliente
        End Get
        Set(ByVal value)
            prvIDCliente = value
        End Set
    End Property
    Public WriteOnly Property NomeCliente()
        Set(ByVal value)
            sNomeCliente = value
        End Set
    End Property
    Public WriteOnly Property NomeContato()
        Set(ByVal value)
            prvNome = value
        End Set
    End Property
    Public WriteOnly Property Telefone()
        Set(ByVal value)
            prvTelefone = value
        End Set
    End Property
    Public WriteOnly Property Celular()
        Set(ByVal value)
            prvCelular = value
        End Set
    End Property
    Public WriteOnly Property Fax()
        Set(ByVal value)
            prvFax = value
        End Set
    End Property
    Public WriteOnly Property Email()
        Set(ByVal value)
            prvEmail = value
        End Set
    End Property
    Public WriteOnly Property Principal()
        Set(ByVal value)
            prvPrincipal = value
        End Set
    End Property
    Public WriteOnly Property Depto()
        Set(ByVal value)
            prvDepto = value
        End Set
    End Property
    Public WriteOnly Property IDDepto()
        Set(ByVal value)
            prvIdDepto = value
        End Set
    End Property
#End Region

#Region "Métodos"
    Public Sub IniciarTela(ByVal modo As eModo)

        modoAtual = modo

    End Sub
    Private Function validarFormulario() As Boolean

        Dim bRetorno As Boolean = True

        If Me.txtNome.Text = String.Empty Then
            sMensagemValidacao = "- Nome do contato obrigatório."
            bRetorno = False
        End If
        If (chkPrincipal.CheckState = CheckState.Checked) Then

        End If

        Return bRetorno

    End Function
    Private Sub CarregarDados()
        Dim dsDadosContato As DataSet
        Try
            If (prvIDCliente > 0 And prvIDContato > 0) Then
                dsDadosContato = objContato.selecionarContato(prvIDContato, prvIDCliente, False)
                If (Not dsDadosContato Is Nothing AndAlso dsDadosContato.Tables("OTH_Contato").Rows.Count > 0) Then
                    With dsDadosContato.Tables("OTH_Contato").Rows(0)
                        txtNome.Text = Persistencia.perConversao.nuloParaVazio(.Item("NomeContato"))
                        txtEmail.Text = Persistencia.perConversao.nuloParaVazio(.Item("email"))
                        txtTelefone.Text = Persistencia.perConversao.nuloParaVazio(.Item("Telefone"))
                        txtCelular.Text = Persistencia.perConversao.nuloParaVazio(.Item("Celular"))
                        txtFax.Text = Persistencia.perConversao.nuloParaVazio(.Item("Fax"))
                        prvIdDepto = Persistencia.perConversao.nuloParaZero(.Item("IDDepartamento"))
                        If (prvIdDepto > 0) Then
                            txtIDepto.Text = prvIdDepto
                            txtDepto.Text = Persistencia.perConversao.nuloParaVazio( _
                                            objDepartamento.retornarCampoDepartamento("Descricao", prvIdDepto).Item("Descricao"))
                        End If
                        chkPrincipal.Checked = Persistencia.perConversao.nuloParaBoleano(.Item("Principal"))

                    End With

                End If
            End If

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
        End Try

    End Sub
#End Region

    Private Sub cmdCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCancelar.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub frmContato2_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.Text = "Contatos - " & sNomeCliente
        CarregarDados()

    End Sub

    Private Sub cmdProcuraDepto_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdProcuraDepto.Click
        Dim sql As String
        Dim frmDialogo As New frmPesquisa

        sql = Me.objDepartamento.sqlConsulta

        With frmDialogo
            .Campos = "IDDepartamento,Descricao"
            .Descricoes = "Código,Descricao"
            .Larguras = "50,300"
            .Sql = sql
            .Titulo = "Pesquisa Departamento"
            .CarregaTela()
            If .DS.Tables(0).Rows.Count > 0 Then
                .ShowDialog()
                If (.DialogResult = Windows.Forms.DialogResult.OK) Then
                    Me.prvIdDepto = .Id
                    Me.txtIDepto.Text = prvIdDepto
                    Me.txtDepto.Text = objDepartamento.retornarCampoDepartamento("descricao", prvIdDepto).Item("descricao").ToString
                End If
            Else
                MsgBox("Registro não encontrado", MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If

        End With
    End Sub

    Private Sub cmdGravar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGravar.Click
        Try
            If (Me.validarFormulario) Then
                If (objContato.salvarContato(prvIDContato, prvIDCliente, txtNome.Text.Trim, prvIdDepto, txtTelefone.Text.Trim, _
                                     txtCelular.Text.Trim, txtFax.Text.Trim, txtEmail.Text.Trim, (chkPrincipal.CheckState = CheckState.Checked))) Then
                    prvIDContato = objContato.IDContato
                    Me.Close()
                End If
            Else
                MsgBox(Me.sMensagemValidacao, MsgBoxStyle.Exclamation, Me.Text)
                Exit Sub
            End If

            Me.DialogResult = Windows.Forms.DialogResult.OK
            Me.Close()

        Catch ex As Exception

            MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)

        End Try
    End Sub
End Class
